package com.haierp.controller.bi;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.haierp.model.bi.BiDailiReport;
import com.haierp.model.bi.BiFavourableMonth;
import com.haierp.model.bi.BiHeroList;
import com.haierp.model.bi.BiLogisticFreight;
import com.haierp.model.bi.BiPurchaseProportion;
import com.haierp.service.bi.IBiFreightService;
import com.haierp.util.NumberUtil;
import com.wangzhixuan.commons.base.BaseController;
import com.wangzhixuan.commons.shiro.ShiroUser;

@Controller
@RequestMapping("/bi/freight")
public class BiFreightController extends BaseController {

	@Autowired
	private IBiFreightService biFreightService;

	@RequestMapping("/selectBiFreightByGmtCreate")
	public String selectBiFreightByGmtCreate(HttpServletRequest request) {
		ShiroUser one = this.getShiroUser();
		List<BiLogisticFreight> freightList = biFreightService.selectBiFreightByGmtCreate(one.getCompanyId());

		Map<String, BiLogisticFreight> shipMap = new HashMap<String,BiLogisticFreight>();
		freightList.forEach(biFreight ->{
			if(shipMap.containsKey(biFreight.getDimensionDate())){
				BiLogisticFreight biFreightNew = shipMap.get(biFreight.getDimensionDate());
				biFreightNew.setCount(biFreightNew.getCount()+1);
				double totalFreight = NumberUtil.formatDouble2(biFreightNew.getLogisticFreightTotal()+biFreight.getLogisticFreight());
				biFreightNew.setLogisticFreightTotal(totalFreight);
				double totalSkuWeight = NumberUtil.formatDouble2(biFreightNew.getPackageWeightTotal()+biFreight.getPackageWeight());
				biFreightNew.setPackageWeightTotal(totalSkuWeight);
				biFreightNew.setPackageNumberTotal(biFreightNew.getPackageNumberTotal()+biFreight.getPackageNumber());
			} else {
				BiLogisticFreight biFreightNew = new BiLogisticFreight();
				biFreightNew.setCount(1);
				biFreightNew.setLogisticFreightTotal(biFreight.getLogisticFreight());
				biFreightNew.setPackageWeightTotal(biFreight.getPackageWeight());
				biFreightNew.setPackageNumberTotal(biFreight.getPackageNumber());
				shipMap.put(biFreight.getDimensionDate(), biFreightNew);
			}
		});

		request.setAttribute("shipMap", shipMap);
		request.setAttribute("freightList", freightList);
		return "bi/freightReport";
	}

	@RequestMapping("/selectBiBuyerPurchaseSuccessRate")
	public String selectBiBuyerPurchaseSuccessRate(HttpServletRequest request) {
		List<BiPurchaseProportion> PurchaseSuccessList = biFreightService.selectBiBuyerPurchaseSuccess();
		Map<String, BiPurchaseProportion> shipMap = new HashMap<String,BiPurchaseProportion>();
		PurchaseSuccessList.forEach(biSuccess ->{
			if(shipMap.containsKey(biSuccess.getDimensionDate())){
				BiPurchaseProportion biFreightNew = shipMap.get(biSuccess.getDimensionDate());
				biFreightNew.setCount(biFreightNew.getCount()+1);
			} else {
				BiPurchaseProportion biFreightNew = new BiPurchaseProportion();
				biFreightNew.setCount(1);
				shipMap.put(biSuccess.getDimensionDate(), biFreightNew);
			}
		});

		request.setAttribute("shipMap", shipMap);
		request.setAttribute("PurchaseSuccessList", PurchaseSuccessList);
		return "bi/buyerPsReport";
	}

	@RequestMapping("/selectHeroicListByTime")
	public String selectHeroicListByTime(HttpServletRequest request) {
		List<BiHeroList> heroicList = biFreightService.selectBiHeroListByTime();
		Map<Long,Object> heroMap = new HashMap<Long,Object>();
		for (BiHeroList biHeroList : heroicList) {
			heroMap.put(biHeroList.getId(), biHeroList);
		}
		request.setAttribute("heroMap", heroMap);
		return "bi/heroicListReport";
	}

	@RequestMapping("/selectdailiOrderRateByDay")
	public String selectdailiOrderRateByDay(HttpServletRequest request) {
		List<BiDailiReport> dailiOrderRateList = biFreightService.selectdailiOrderRateByDay();
		List<BiDailiReport> dailiOrderRateMonthList = biFreightService.selectdailiOrderRateByMonth();
		dailiOrderRateMonthList.forEach(biDaili ->{
			biDaili.setDailiRate(NumberUtil.formatDouble2(biDaili.getDailiRate()*100));
			biDaili.setSaleOrderTotalPrice(NumberUtil.formatDouble2(biDaili.getSaleOrderTotalPrice()));
			biDaili.setSaleOrderDailiPrice(NumberUtil.formatDouble2(biDaili.getSaleOrderDailiPrice()));
			biDaili.setDailiPriceRate(NumberUtil.formatDouble2(biDaili.getDailiPriceRate()*100));
		});
		request.setAttribute("dailiOrderRateList", dailiOrderRateList);
		request.setAttribute("dailiOrderRateMonthList", dailiOrderRateMonthList);
		return "bi/dailiOrderRateReport";
	}

	@RequestMapping("/selectBiFavourableByMonth")
	public String selectBiFavourableByMonth(HttpServletRequest request) {
		List<BiFavourableMonth> favourableMonthList = biFreightService.selectBiFavourableByMonth();
		request.setAttribute("favourableMonthList", favourableMonthList);
		return "bi/favourableMonthReport";
	}
}
